package org.celery.modules.shop.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.shop.entity.ShopDept;
import org.celery.modules.shop.vo.ShopDeptVO;
import org.celery.modules.shop.wrapper.ShopDeptWrapper;
import org.celery.modules.shop.service.IShopDeptService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺部门关联表 控制器
 *
 * @author Celery
 * @since 2023-09-12
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-shop-dept/shop-dept")
@Api(value = "店铺部门关联表", tags = "店铺部门关联表接口")
public class ShopDeptController extends BladeController {

	private final IShopDeptService shopDeptService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入shopDept")
	public R<ShopDeptVO> detail(ShopDept shopDept) {
		ShopDept detail = shopDeptService.getOne(Condition.getQueryWrapper(shopDept));
		return R.data(ShopDeptWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 店铺部门关联表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入shopDept")
	public R<IPage<ShopDeptVO>> list(ShopDept shopDept, Query query) {
		IPage<ShopDept> pages = shopDeptService.page(Condition.getPage(query), Condition.getQueryWrapper(shopDept));
		return R.data(ShopDeptWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 店铺部门关联表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入shopDept")
	public R<IPage<ShopDeptVO>> page(ShopDeptVO shopDept, Query query) {
		IPage<ShopDeptVO> pages = shopDeptService.selectShopDeptPage(Condition.getPage(query), shopDept);
		return R.data(pages);
	}

	/**
	 * 新增 店铺部门关联表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入shopDept")
	public R save(@Valid @RequestBody ShopDept shopDept) {
		return R.status(shopDeptService.save(shopDept));
	}

	/**
	 * 修改 店铺部门关联表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入shopDept")
	public R update(@Valid @RequestBody ShopDept shopDept) {
		return R.status(shopDeptService.updateById(shopDept));
	}

	/**
	 * 新增或修改 店铺部门关联表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入shopDept")
	public R submit(@Valid @RequestBody ShopDept shopDept) {
		return R.status(shopDeptService.saveOrUpdate(shopDept));
	}

	
	/**
	 * 删除 店铺部门关联表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(shopDeptService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 保存列表
	 */
	@PostMapping("/save-list")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "保存列表")
	public R saveList(@ApiParam(value = "部门id", required = true) @RequestParam Long deptId,
					  @ApiParam(value = "店铺ids", required = true) @RequestParam String shopIds) {
		List<ShopDept> shopDeptList = shopDeptService.list(Wrappers.<ShopDept>lambdaQuery()
				.eq(ShopDept::getDeptId, deptId)
		);

		List<String> shopIdList = shopDeptList.stream().map(item -> String.valueOf(item.getShopId())).collect(Collectors.toList());
		List<String> shopIdNewList = Func.toStrList(shopIds);

		List<String> addIdList = new ArrayList<>();
		shopIdNewList.forEach(shopId -> {
			if (!shopIdList.contains(shopId)) {
				addIdList.add(shopId);
			}
		});

		List<String> removeIdList = new ArrayList<>();
		shopIdList.forEach(shopId -> {
			if (!shopIdNewList.contains(shopId)) {
				removeIdList.add(shopId);
			}
		});

		if (Func.isNotEmpty(removeIdList)) {
			shopDeptService.remove(Wrappers.<ShopDept>lambdaQuery().eq(ShopDept::getDeptId, deptId).in(ShopDept::getShopId, removeIdList));
		}
		if (Func.isNotEmpty(addIdList)) {
			List<ShopDept> addList = new ArrayList<>();
			addIdList.forEach(id -> {
				ShopDept shopDept = new ShopDept();
				shopDept.setDeptId(deptId);
				shopDept.setShopId(Long.valueOf(id));
				addList.add(shopDept);
			});
			shopDeptService.saveOrUpdateBatch(addList);
		}
		return R.status(true);
	}

	
}
